package main

import (
	"fmt"
	"github.com/julienschmidt/httprouter"
	"log"
	"net/http"
)

type MyHandler struct{}

func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello world!")
}

/**
https web服务
*/
func httpsServer() {
	handler := MyHandler{}
	server := http.Server{
		Addr:    "127.0.0.1:8089", //https://127.0.0.1:8089/
		Handler: &handler,
	}
	err := server.ListenAndServeTLS("cert.pem", "key.pem")
	if err != nil {
		log.Fatal(err)
	}
	//server.ListenAndServe()
}

type HelloHandler struct{}

func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello!")
}

type WorldHandler struct{}

func (h *WorldHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "World!")
}

/**
多个处理器
*/
func multiHandler() {
	helloHandler := HelloHandler{}
	worldHandler := WorldHandler{}
	server := http.Server{
		Addr: "127.0.0.1:8089",
	}
	http.Handle("/hello", &helloHandler)
	http.Handle("/world", &worldHandler)
	server.ListenAndServe()
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello!")
}
func world(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "World!")
}

/**
处理器函数
*/
func handleFun() {
	server := http.Server{
		Addr: "127.0.0.1:8089",
	}
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/world", world)
	server.ListenAndServe()
}

/**
使用第三方多路复用器 julienschmidt/httprouter
go get github.com/julienschmidt/httprouter
*/
func useHttpRouter() {
	mux := httprouter.New()
	server := http.Server{
		Addr:    "127.0.0.1:8089",
		Handler: mux,
	}
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/world", world)
	server.ListenAndServe()
}
func main39() {
	//httpsServer()
	//multiHandler()
	handleFun()
}
